﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Customer_Profile.Admin
{
    public partial class CodeManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CodeTypeDao typedao = new CodeTypeDao();                
                CodeDao codedao = new CodeDao();
                DataSet dsType = new DataSet();
                DataSet dsCode = new DataSet();
                TreeNode rootNode = new TreeNode();
                rootNode.Text = "Code";
                rootNode.Value = "-1";
                rootNode.Expanded = true;
                rootNode.ShowCheckBox = true;
                dsType = typedao.GetList();                
                if (dsType != null && dsType.Tables.Count > 0)
                {                    
                    foreach (DataRowView row in dsType.Tables[0].DefaultView)
                    {
                        TreeNode typeNode = new TreeNode();
                        typeNode.Text = row["CodeTypeName"].ToString();
                        string strID = row["ID"].ToString();
                        typeNode.Value = strID + ",G";
                        typeNode.Expanded = true;
                        typeNode.ShowCheckBox = true;
                        dsCode = codedao.GetList(strID);
                        rootNode.ChildNodes.Add(typeNode);
                        if (dsCode != null && dsCode.Tables.Count > 0)
                        {
                            foreach (DataRowView rowcode in dsCode.Tables[0].DefaultView)
                            {
                                TreeNode node = new TreeNode();
                                node.Text = rowcode["CodeName"].ToString();
                                node.Value = rowcode["Guid"].ToString()+",E";
                                node.ShowCheckBox = true;
                                typeNode.ChildNodes.Add(node);
                            }
                        }                        
                    }
                }
                treeMenu.Nodes.Add(rootNode);
            }
        }

        protected void ibtnAdd_ServerClick(object sender, EventArgs e)
        {
            if (treeMenu.CheckedNodes.Count == 0)
            {
                KVB.Common.JavaScript.ViewAlertMsg("you must select!");
                return;
            }
            TreeNode node = treeMenu.CheckedNodes[0];
            string selectId = node.Value;
            string selectName = node.Text;
            if (selectId.Trim().Equals("-1"))
            {
                Response.Redirect("AddCodeType.aspx");
            }
            else
            {
                Response.Redirect("AddCode.aspx?id="+selectId,true);
            }
        }

        protected void ibtnModify_ServerClick(object sender, EventArgs e)
        {
            if (treeMenu.CheckedNodes.Count == 0)
            {
                KVB.Common.JavaScript.ViewAlertMsg("you must select!");
                return;
            }
            TreeNode node = treeMenu.CheckedNodes[0];
            string selectId = node.Value;
            string selectName = node.Text;
            CodeTypeDao typedao = new CodeTypeDao();
           //bool flag = typedao.IsType(selectId);
            bool flag = false;
            
            if (selectId.Split(',')[1].ToString().Equals("G"))
                flag = true;
            else
                flag = false;

            if (selectName.Equals("Code"))
            {
                Comm.ViewAlertMsg("Base Node Can't Edit");
                return;
            }
            if (flag)
                Response.Redirect("TypeModify.aspx?id=" + selectId.Split(',')[0].ToString(), true);
            else
                Response.Redirect("CodeModify.aspx?id=" + selectId.Split(',')[0].ToString(), true);
        }
    }
}
